#!/bin/sh
# PCP QA Test No. 1549
# Exercise pmrep instance selection.
#
# Copyright (c) 2015-2021 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

if [ $PCP_PLATFORM = freebsd ]
then
    # workaround datetime.strftime() in Python that is busted when
    # the timezone|timeinfo is, er, Australia/Someplace, for at least
    # some parts of the year
    #
    version=`uname -rs | sed -e 's/FreeBSD //'`
    case "$version"
    in
	14.[0-3]-*)
	    _notrun "datetime.strftime() botch"
	    # NOTREACHED
	    ;;
    esac
fi

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

log="--archive $here/archives/proc -z"
log2="--archive $here/archives/20130706 -z -O 30m"

# real QA test starts here
echo "== user-requested instance handling"
pmrep -s 1 -i wrong $log2 disk.dev.read
pmrep -s 1 -i wrong $log2 mem.util.free
pmrep -s 1 -i wrong $log2 disk.dev.read mem.util.free
pmrep -s 1 -i wrong,sda $log2 disk.dev.read
pmrep -s 1 -i wrong,sda,wrong $log2 disk.dev.read
pmrep -s 1 -i '"1 minute",wrong,sda,"5 minute","still,wrong"' $log2 disk.dev.read kernel.all.load
pmrep -s 1 -i '"1 minute",wrong,"5 minute"' $log2 kernel.all.load,,"1 minute",,,16
pmrep -s 1 -i "1 minute" -i wrong -i "5 minute" $log2 kernel.all.load
pmrep -s 1 $log2 disk.dev.read,,sda
pmrep -s 1 $log2 disk.dev.read,,sda,,,16
pmrep -s 1 $log2 disk.dev.read,,"'sda','sdb'"
pmrep -s 1 $log2 disk.dev.read,,"'sda','sdb'",,,16
echo '[test]' > $tmp.iconfig
echo 'instances = wrong' >> $tmp.iconfig
echo 'sysfork = kernel.all.sysfork' >> $tmp.iconfig
echo 'mem.util.free = free,,,,16' >> $tmp.iconfig
# We want: kernel.all.load = load,"'1 minute','5 minute','15 min'",,,16
echo "kernel.all.load = load,\"'1 minute','5 minute','15 min'\",,,16" >> $tmp.iconfig
echo 'disk.dev.read = read,sda,,,16' >> $tmp.iconfig
echo 'disk.dev.write = write,wrong,,,16' >> $tmp.iconfig
pmrep -s 1 $log2 -c $tmp.iconfig :test
pmrep -s 1 -i "'.*'" $log2 disk.dev.read
pmrep -s 1 -i "'.*sda.*'" $log2 disk.dev.read
echo '[test]' > $tmp.iconfig
echo 'instances = sd.*' >> $tmp.iconfig
echo 'disk.dev.read = read,,,,8' >> $tmp.iconfig
echo 'disk.dev.write = write,vd.*,,,8' >> $tmp.iconfig
pmrep -s 1 $log2 -c $tmp.iconfig :test
echo '[test]' > $tmp.iconfig
echo 'test = disk.dev.read' >> $tmp.iconfig
echo 'test.label = " sd.* "' >> $tmp.iconfig
echo 'test.instances = "sd.*"' >> $tmp.iconfig
pmrep -s 1 $log2 -c $tmp.iconfig -l '|' :test

echo "=== more instance handling"
cat > $tmp.iiconfig <<EOF
[test1] # fails
instances = xxx,yyy,zzz
foo = disk.dev.read
foo.instances = wrong

[test2] # fails
instances = sda,sdc
foo = disk.dev.read
foo.instances = sdx

[test3] # fails
instances = sda
disk.dev.read = sdx,sdy

[test4] # 1: fails, 2,3: works
instances = sda
disk.dev.read = l1,sdx
disk.dev.write = l2
disk.dev.total = l3,

[test5] # works
instances = sdx
foo = disk.dev.read
foo.instances = sda

[test6] # works
instances = sda,sdx
foo = disk.dev.read
foo.instances = sda

[test7] # works
instances = sdx
disk.dev.read = label,'sda,sdx'
EOF
pmrep -s 1 $log2 -c $tmp.iiconfig :test1 | sed -e "s,u',',g"
pmrep -s 1 $log2 -c $tmp.iiconfig :test2 | sed -e "s,u',',g"
pmrep -s 1 $log2 -c $tmp.iiconfig :test3 | sed -e "s,u',',g"
pmrep -s 1 $log2 -c $tmp.iiconfig :test4
pmrep -s 1 $log2 -c $tmp.iiconfig :test5
pmrep -s 1 $log2 -c $tmp.iiconfig :test6
pmrep -s 1 $log2 -c $tmp.iiconfig :test7

echo "== testing improved instance handling"
pmrep -C -U $log -i 1           proc.psinfo.pid
pmrep -C -U $log -i 2           proc.psinfo.pid
pmrep -C -U $log -i 1 -i 2 -i Z proc.psinfo.pid
pmrep -C -U $log -i 1,2,570     proc.psinfo.pid
pmrep -C -U $log -i 1           proc.psinfo.pid,,2
pmrep -C -U $log -i pmcd        proc.psinfo.pid
pmrep -C -U $log -i XpmcdX      proc.psinfo.pid
pmrep -C -U $log -i '.*pmcd.*'  proc.psinfo.pid
pmrep -C -U $log -i pmcd        proc.psinfo.pid,,1

pmrep -C -U $log -i 0           disk.dev.write
pmrep -C -U $log -i sda         disk.dev.write
pmrep -C -U $log -i 0           disk.dev.write,,sda

pmrep -u -U $log -i pmcd -j -1  proc.psinfo.pid

# success, all done
echo "== done"
status=0
exit
